package com.wf.smartcomm.server.serviceImpl;


import java.math.BigInteger;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Decorate;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.DecorateService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午4:15:38 
 * 
 */
@Service
@Transactional
public class DecorateServiceImpl implements DecorateService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Decorate decorate) {
		decorate.setObjectId(IdUtils.uuid());
		decorate.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		decorate.setUpdateAt(decorate.getCreatedAt());
		factory.getCurrentSession().persist(decorate);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Decorate where user = :user");
		query.setParameter("user", user);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Decorate decorate where decorate.user = :user order by decorate.createdAt");
		query.setParameter("user", user);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void list(Village village,Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select count(*) from t_decorate where hid in (select objectId from t_house where vid =:vid)");
		query.setParameter("vid", village.getObjectId());
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createSQLQuery("select * from t_decorate where hid in (select objectId from t_house where vid =:vid) order by updateAt").addEntity(Decorate.class);
		query.setParameter("vid", village.getObjectId());
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void update(Decorate decorate) {
		decorate.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(decorate);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void queryByKey(Village village,String key, Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select count(*) from t_decorate where hid in (select objectId from t_house where vid =:vid and (disAddress like :key or name like :key))");
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+key+"%");
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createSQLQuery("select * from t_decorate where hid in (select objectId from t_house where vid =:vid and (disAddress like :key or name like :key)) order by updateAt").addEntity(Decorate.class);
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+key+"%");
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void check(String id) {
		String sql = "update t_decorate set status = 1 where objectId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void end(String id) {
		String sql = "update t_decorate set status = 2, bailStatus = 2 where objectId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Decorate get(String id) {
		Query query = factory.getCurrentSession().createQuery("from Decorate where objectId =:id");
		query.setParameter("id", id);
		return (Decorate) query.uniqueResult();
	}

	@Override
	public void delete(Decorate decorate) {
		factory.getCurrentSession().delete(decorate);
	}

	@Override
	public void listByHouse(House house, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Decorate where house =:house order by createdAt desc");
		query.setParameter("house", house);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}
}
